package cn.wolfcode.shop.cloud.mapper;

import cn.wolfcode.shop.cloud.domain.OrderInfo;
import org.apache.ibatis.annotations.*;

/**
 * Created by wolfcode-lanxw
 */
@Mapper
public interface OrderInfoMapper {

    @Insert("insert into t_order_info(order_no,user_id,good_id,delivery_addr_id,good_name,good_img,good_count,good_price,seckill_price,status,create_date,pay_date) values(#{orderNo},#{userId},#{goodId},#{deliveryAddrId},#{goodName},#{goodImg},#{goodCount},#{goodPrice},#{seckillPrice},#{status},#{createDate},#{payDate})")
    void insert(OrderInfo orderInfo);

    @Select("select * from t_order_info where order_no = #{orderNo} and user_id = #{userId}")
    OrderInfo selectByPrimaryKey(@Param("orderNo") String orderNo, @Param("userId") Long userId);

    /**
     * 根据订单状态来修改订单状态
     * 如果订单未支付则修改成功, 且返回受影响的行数 > 0
     * 如果订单已支付或者已超时, 则都修改失败, 且返回受影响的行数 <= 0
     * @param orderNo
     * @return
     */
    @Update("update t_order_info set status = 3 where order_no = #{orderNo} and status = 0")
    int updateStatus(String orderNo);

    /**
     * 更新支付状态
     * @param orderNo
     */
    @Update("update t_order_info set status = 1, pay_date = now() where order_no = #{orderNo} and status = 0")
    void updatePayStatus(String orderNo);
}
